package com.topscore.uke.mvp.presenters;

import com.topscore.uke.entities.ActivitiesNewsData;
import com.topscore.uke.mvp.contractors.GetActivitiesContractor;
import com.topscore.uke.mvp.model.GetActivitiesModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.utils.UserInfoManager;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 71468 on 2018/3/29.
 */

public class GetActivitiesPresenter implements GetActivitiesContractor, GetActivitiesContractor.Presenter {
    private View mView;
    private Model mModel;

    private Map<String, Object> mRequest;

    private Map<String, Object> mPageParams;

    private int mCurrentPage = 1;
    private final int _PageSize = 15;

    public GetActivitiesPresenter(View view) {
        this.mView = view;

        mRequest = new HashMap<>();
        mRequest.put("loginName", UserInfoManager.getUserInfo().getLoginName());
        mPageParams = new HashMap<>();
        mPageParams.put("pageSize", _PageSize);
        mPageParams.put("page", mCurrentPage);
        mRequest.put("pageParams", mPageParams);
    }

    @Override
    public void getActivitiesData() {
        mView.onLoading("");
        mCurrentPage = 1;
        mPageParams.put("page", mCurrentPage);
        this.mModel = new GetActivitiesModel(new CallBack() {
            @Override
            public void onSuccess(BaseResponse<ActivitiesNewsData> response) {
                if (!response.getData().getDataList().isEmpty()) {
                    mView.getListAdapter().setNewData(response.getData().getDataList());
                    if (response.getData().getDataList().size() < _PageSize) {
                        mView.getListAdapter().loadMoreComplete();
                        mView.getListAdapter().loadMoreEnd();
                    }
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mModel.getData(mRequest);
    }

    @Override
    public void loadNextPage() {
        int page = mCurrentPage + 1;
        mPageParams.put("page", page);
        mModel = new GetActivitiesModel(new CallBack() {
            @Override
            public void onSuccess(BaseResponse<ActivitiesNewsData> response) {
                if (!response.getData().getDataList().isEmpty()) {
                    mCurrentPage = page;
                    mView.getListAdapter().addData(response.getData().getDataList());
                    if (response.getData().getDataList().size() < _PageSize) {
                        mView.getListAdapter().loadMoreComplete();
                        mView.getListAdapter().loadMoreEnd();
                    }
                } else {
                    mView.getListAdapter().loadMoreComplete();
                    mView.getListAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.getListAdapter().loadMoreComplete();
                mView.getListAdapter().loadMoreFail();
            }

            @Override
            public void onComplete() {
            }
        });
        mModel.getData(mRequest);
    }
}
